﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Dal;

namespace Control.Bus
{
    public class BusProduct
    {
        public int ID { get; set; }
        public string ProductCode { get; set; }
        public string ProductName { get; set; }
        private BusShelf shelf;
        public BusShelf Shelf
        {
            set { shelf = value; }
            get
            {
                if (shelf == null)
                    shelf = new BusShelf();
                return shelf;
            }
        }
        public string ShelfName
        {
            get { return Shelf.ShelfName+shelf.Tray; }
        }

        public static BusProduct GetProdByCode(string MaProd)
        {
            return DalProduct.GetProdByCode(MaProd);
        }

        public int GetTonKho()
        {
            List<BusWareHouse> whs = DalWareHouse.GetTonKhoByProduct(this);
            try
            {
                return whs[0].Store;
            }
            catch
            {
                return 0;
            }
        }

        public static List<BusProduct> GetAll()
        {
            return DalProduct.GetAll();
        }

        public void GetProperty()
        {
            BusProduct p = GetProdByCode(this.ProductCode);
            try
            {
                ID = p.ID;
                ProductName = p.ProductName;
                Shelf = p.Shelf;
            }
            catch { ID = 0; }
        }

        public void Insert()
        {
            this.ID = DalProduct.Insert(this);
        }
        public void Update()
        {
            DalProduct.Update(this);
        }

        public void Delete()
        {
            DalProduct.Delete(this);
        }

        //internal int GetTonKho(DateTime billdate)
        //{
        //    DateTime lastday = billdate.AddDays(-1);
        //    List<BusWareHouse> whs = DalWareHouse.GetTonKhoByProduct(this);
        //    for (int i = 0; i < whs.Count; i++)
        //    {
        //        if (whs[i].Date.ToShortDateString().CompareTo(lastday.ToShortDateString()) == 0)
        //            return whs[i].Store;
        //    }
        //    return 0;
        //    //List<BusWareHouse> resuils = new List<BusWareHouse>();
        //    //foreach (BusWareHouse item in whs)
        //    //{
        //    //    if
        //    //}
        //    //try
        //    //{
        //    //    return whs[0].Store;
        //    //}
        //    //catch
        //    //{
        //    //    return 0;
        //    //}
        //}

        public int GetTonKho(DateTime toDate)
        {
            DateTime newdate = new DateTime(toDate.Year, toDate.Month, toDate.Day, 0, 0, 1);
            List<BusWareHouse> whs = DalWareHouse.GetTonKhoByProduct(this,newdate);


            try
            {
                return whs[0].Store;
            }
            catch
            {
                return 0;
            }
        }

        public static List<BusProduct> Find(string p)
        {
            return DalProduct.Find(p);
        }
    }
}
